package com.dinzeer.legendreliclib.lib.compat.slashblade;

import com.dinzeer.legendreliclib.lib.compat.slashblade.SlashBladeCompatEntities;
import com.dinzeer.legendreliclib.lib.compat.slashblade.entity.swordrain.BaseSwordRainEntity;
import com.dinzeer.legendreliclib.lib.util.slashblade.SlashBladeUtil;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;

import java.util.ArrayList;
import java.util.List;

/**
 * 剑雨生成器工具类，用于生成特定布局的定向停止剑雨
 * 从对面生物和玩家对视的角度看布局如下：
 *          A
 *        B   B
 *      C       C
 * 玩家(P)位于中心
 */
public class SwordRainGenerator {

    /**
     * 生成五个定向停止剑雨，形成特定布局
     * 从对面生物和玩家对视的角度看布局如下：
     *          A
     *        B   B
     *      C       C
     * 玩家(P)位于中心
     *
     * @param player 玩家实体
     * @param world  世界
     * @param damage 剑雨伤害
     * @return 生成的剑雨实体列表
     */
    public static List<BaseSwordRainEntity> generateFivePointSwordRain(LivingEntity player, Level world,int damage) {
        if (world.isClientSide)return new ArrayList<>();
        List<BaseSwordRainEntity> swordRains = new ArrayList<>();

        // 获取玩家位置和朝向
        Vec3 playerPos = player.position();
        float playerYaw = player.getYRot();
        float playerPitch = player.getXRot();

        // 计算基础偏移量
        double horizontalDistance = 2.0; // 水平距离
        double verticalDistance = 2.5;   // 垂直距离

        // 计算方向向量
        Vec3 forward = Vec3.directionFromRotation(playerPitch, playerYaw).normalize();
        Vec3 right = Vec3.directionFromRotation(0, playerYaw + 90).normalize();
        Vec3 up = new Vec3(0, 1, 0);

        // 计算五个点的位置（从对面视角看）
        // 点A - 正上方
        Vec3 pointA = playerPos.add(up.scale(verticalDistance * 1.5));

        // 点B - 两侧（稍微上方）
        Vec3 pointBLeft = playerPos.add(right.scale(-horizontalDistance))
                .add(up.scale(verticalDistance * 0.8));

        Vec3 pointBRight = playerPos.add(right.scale(horizontalDistance))
                .add(up.scale(verticalDistance * 0.8));

        // 点C - 两侧（稍微下方）
        Vec3 pointCLeft = playerPos.add(right.scale(-horizontalDistance * 1.5))
                .add(up.scale(verticalDistance * 0.3));

        Vec3 pointCRight = playerPos.add(right.scale(horizontalDistance * 1.5))
                .add(up.scale(verticalDistance * 0.3));

        int colorCode=SlashBladeUtil.getColorCode(player.getMainHandItem());
        // 创建剑雨实体
        BaseSwordRainEntity swordRainA = createSwordRainAtPosition(world, player, pointA,1,colorCode, damage);
        BaseSwordRainEntity swordRainBLeft = createSwordRainAtPosition(world, player, pointBLeft,2,colorCode, damage);
        BaseSwordRainEntity swordRainBRight = createSwordRainAtPosition(world, player, pointBRight,3,colorCode, damage);
        BaseSwordRainEntity swordRainCLeft = createSwordRainAtPosition(world, player, pointCLeft,4,colorCode, damage);
        BaseSwordRainEntity swordRainCRight = createSwordRainAtPosition(world, player, pointCRight,5,colorCode, damage);

        // 设置剑雨朝向（朝向玩家前方）
        setSwordRainDirection(swordRainA, forward);
        setSwordRainDirection(swordRainBLeft, forward);
        setSwordRainDirection(swordRainBRight, forward);
        setSwordRainDirection(swordRainCLeft, forward);
        setSwordRainDirection(swordRainCRight, forward);

        // 添加到列表
        swordRains.add(swordRainA);
        swordRains.add(swordRainBLeft);
        swordRains.add(swordRainBRight);
        swordRains.add(swordRainCLeft);
        swordRains.add(swordRainCRight);

        // 将实体添加到世界
        for (BaseSwordRainEntity swordRain : swordRains) {
            world.addFreshEntity(swordRain);
        }

        return swordRains;
    }

    /**
     * 在指定位置创建定向停止剑雨实体
     *
     * @param world    世界
     * @param owner    所有者
     * @param position 位置
     * @return 创建的剑雨实体
     */
    private static BaseSwordRainEntity createSwordRainAtPosition(Level world, Entity owner, Vec3 position,int Delay,int color,int damage) {
        BaseSwordRainEntity swordRain = new BaseSwordRainEntity(
                SlashBladeCompatEntities.BASE_SWORD_RAIN.get(),
                world
        );

        // 设置位置
        swordRain.setPos(position.x, position.y, position.z);
        // 设置所有者
        swordRain.setOwner(owner);
        swordRain.setDamage(damage);
        swordRain.setDelay(Delay);
        swordRain.setColor(color);
        // 设置向前方向
        swordRain.setForward(true);

        return swordRain;
    }

    /**
     * 设置剑雨实体的朝向
     *
     * @param swordRain 剑雨实体
     * @param direction 方向向量
     */
    private static void setSwordRainDirection(BaseSwordRainEntity swordRain, Vec3 direction) {
        // 计算偏航角（yaw）和俯仰角（pitch）
        double horizontalDistance = Math.sqrt(direction.x * direction.x + direction.z * direction.z);
        float yaw = (float) Math.toDegrees(Math.atan2(direction.x, direction.z));
        float pitch = (float) Math.toDegrees(Math.atan2(direction.y, horizontalDistance));

        // 设置实体朝向
        swordRain.setYRot(yaw);
        swordRain.setXRot(pitch);
    }

    /**
     * 生成防御阵型剑雨（围绕玩家形成保护圈）
     *
     * @param player     玩家实体
     * @param world      世界
     * @param count      剑雨数量
     * @param radius     半径
     * @param height     高度
     * @return 生成的剑雨实体列表
     */
    public static List<BaseSwordRainEntity> generateDefensiveSwordRain(
            LivingEntity player, Level world, int count, double radius, double height) {
        List<BaseSwordRainEntity> swordRains = new ArrayList<>();

        Vec3 playerPos = player.position();

        for (int i = 0; i < count; i++) {
            // 计算角度
            double angle = 2 * Math.PI * i / count;

            // 计算位置
            double xOffset = radius * Math.cos(angle);
            double zOffset = radius * Math.sin(angle);

            Vec3 position = playerPos.add(xOffset, height, zOffset);

            // 创建剑雨
            BaseSwordRainEntity swordRain = createSwordRainAtPosition(world, player, position,1, SlashBladeUtil.getColorCode(player.getMainHandItem()),10);

            // 设置朝向（指向外侧）
            Vec3 outwardDirection = position.subtract(playerPos).normalize();
            setSwordRainDirection(swordRain, outwardDirection);

            swordRains.add(swordRain);
            world.addFreshEntity(swordRain);
        }

        return swordRains;
    }
}